package id.amikom.jogjabankfinder.activity;

import java.util.ArrayList;

import org.json.JSONObject;

import com.google.android.gms.maps.model.LatLng;

import id.amikom.jogjabankfinder.GPSHelper;
import id.amikom.jogjabankfinder.JSONHelper;
import id.amikom.jogjabankfinder.R;
import id.amikom.jogjabankfinder.Utils;
import id.amikom.jogjabankfinder.entity.Bank;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;

public class ListBank extends Activity implements OnClickListener, LocationListener, OnItemSelectedListener
{

	private JSONHelper			json;
	private GPSHelper			gps;
	private LocationManager		locManager;
	private Location			location;
	private Context				context;

	private LatLng				myLocation;
	private int					loc;

	private Spinner				spListBank;
	private Button				btnGetDirection;
	private EditText			txtMyLocation;

	private ArrayList<Bank>		listBank;
	private ArrayList<String>	listBankSpinner;
	private ProgressDialog		pDialog;

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_list_bank);

		spListBank = (Spinner) findViewById(R.id.spBankListBank);
		btnGetDirection = (Button) findViewById(R.id.btnListBankGetDirection);
		txtMyLocation = (EditText) findViewById(R.id.txtMyPosition);
		btnGetDirection.setOnClickListener(this);
		spListBank.setOnItemSelectedListener(this);

		context = this;
		locManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
		setupGPS();

		json = new JSONHelper();

		new AsyncTaskListBank().execute();
	}

	private void setupGPS()
	{
		gps = new GPSHelper(context, locManager, this);
		location = gps.getLocation();

		if (location != null)
		{
			myLocation = new LatLng(location.getLatitude(), location.getLongitude());
			onLocationChanged(location);
		}
		txtMyLocation.setText(Utils.getRoundTwoDecimal(myLocation.latitude) + ","
				+ Utils.getRoundTwoDecimal(myLocation.longitude));
	}

	private class AsyncTaskListBank extends AsyncTask<Void, Void, Void>
	{

		@Override
		protected Void doInBackground(Void... params)
		{
			String URL = "http://jogjabankfinder.tk/lokasibank.php";

			JSONObject jObj = json.getJSONFromUrl(URL);
			listBank = json.getAllBankLocation(jObj);

			return null;
		}

		@Override
		protected void onPostExecute(Void result)
		{
			pDialog.dismiss();

			runOnUiThread(new Runnable()
			{

				@Override
				public void run()
				{
					// TODO Auto-generated method stub
					listBankSpinner = new ArrayList<String>();
					listBankSpinner.add("Daftar Bank");

					for (int i = 0; i < listBank.size(); i++)
					{
						listBankSpinner.add(listBank.get(i).getName());
					}
				}
			});

			tampilSpinner();
			super.onPostExecute(result);
		}

		@Override
		protected void onPreExecute()
		{
			// TODO Auto-generated method stub
			super.onPreExecute();

			pDialog = new ProgressDialog(ListBank.this);
			pDialog.setMessage("Loading....");
			pDialog.setCancelable(true);
			pDialog.show();
		}

	}

	private void tampilSpinner()
	{
		// TODO Auto-generated method stub
		ArrayAdapter<String> bankAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item,
				listBankSpinner);
		bankAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

		spListBank.setAdapter(bankAdapter);

	}

	@Override
	public void onClick(View v)
	{
		// TODO Auto-generated method stub
		if (v == btnGetDirection)
		{
			Bundle bundle = new Bundle();

			bundle.putString("to", listBank.get(loc).getName());
			bundle.putDouble("start_lat", myLocation.latitude);
			bundle.putDouble("start_lng", myLocation.longitude);
			Utils.TRACE("loc " + loc);
			bundle.putDouble("end_lat", listBank.get(loc).getLat());
			bundle.putDouble("end_lng", listBank.get(loc).getLng());

			Intent i = new Intent(ListBank.this, Navigasi.class);
			i.putExtras(bundle);
			startActivity(i);
			finish();
		}

	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event)
	{
		// TODO Auto-generated method stub
		if (keyCode == KeyEvent.KEYCODE_BACK)
		{
			startActivity(new Intent(this, MenuUtama.class));
			finish();
		}
		return super.onKeyDown(keyCode, event);
	}

	@Override
	public void onLocationChanged(Location location)
	{
		// TODO Auto-generated method stub
		if (myLocation != null)
		{
			myLocation = new LatLng(location.getLatitude(), location.getLongitude());
		}
	}

	@Override
	public void onProviderDisabled(String provider)
	{
		// TODO Auto-generated method stub

	}

	@Override
	public void onProviderEnabled(String provider)
	{
		// TODO Auto-generated method stub

	}

	@Override
	public void onStatusChanged(String provider, int status, Bundle extras)
	{
		// TODO Auto-generated method stub

	}

	@Override
	public void onItemSelected(AdapterView<?> arg0, View arg1, int location, long arg3)
	{
		// TODO Auto-generated method stub
		if (location != 0)
		{
			loc = location;
			loc = loc - 1;
		}

	}

	@Override
	public void onNothingSelected(AdapterView<?> arg0)
	{

	}

}
